<template>
    <el-form :model="dizhi" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="用户id" prop="yhid">
                    <el-input v-model="dizhi.yhid" placeholder="请输入用户id" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="地址" prop="address">
                    <el-input v-model="dizhi.address" placeholder="请输入地址" clearable/>
                </el-form-item>
            </el-col>
        </el-row>
    </el-form>
    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup>
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
    "closeDialog",	// 关闭 dialog
    "flushPage"		// 刷新页面
]);

const dizhi = ref({
    dzid: '',
    yhid: '',
    address: ''
})

const handleUpdateData = (obj) => {
    dizhi.value = obj;
}

// 暴露给父组件使用的方法
defineExpose({
    handleUpdateData,
})

// 取消
const doCancel = () => {
    emits("closeDialog");
}

// 更新
const doSave = async () => {
    const {data} = await axios.put('/api/dizhi', dizhi.value);
    if (data.code === 200) {
        emits("flushPage");				// 触发事件： 刷新页面
        emits("closeDialog");			// 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>

<style scoped>

</style>